package com.iuchannel.android.sample;

import com.iuchannel.android.provider.Const.YT;

import android.net.Uri;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.AsyncQueryHandler;
import android.database.Cursor;

import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends ListActivity {

	private final static String USER_NAME="chanyhan";
	ListView mListView;
	SimpleCursorAdapter mAdapter;
	AsyncQueryHandler mHandler;
	
	Uri mUri;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mUri=Uri.parse("content://"+getPackageName()+".YouTubeProvider/"+YT.TABLE_USER+"/"+USER_NAME);
		mListView=getListView();
		
		mAdapter=new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, 
				new String[]{YT.PRJ_TITLE}, 
				new int[]{android.R.id.text1}, 
				SimpleCursorAdapter.NO_SELECTION){
			
		};
		
		mListView.setAdapter(mAdapter);
		
		mHandler=new AsyncQueryHandler(getContentResolver()){
			@Override
			protected void
			onQueryComplete(int token, Object cookie, Cursor cursor){
				Log.d("Test", "onQueryComplete");
				if(cursor==null){
					Log.d("Test", "Cursor=null");
				}
				
				if(cursor!=null && cursor.getCount()>0){
					Log.d("Test", "Cursor>0");
					mAdapter.swapCursor(cursor);
					mListView.invalidateViews();
				}
			};
		};
		
		mHandler.startQuery(0, null, mUri, null, null, null, null);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
